package org.example.ttms.entityClass.train;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import java.io.*;
import java.util.*;

public class Station {
    private String name;
    private int routeNum;

    public Station() {
    }

    public Station(String name, int routeNum) {
        this.name = name;
        this.routeNum = routeNum;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getRouteNum() {
        return routeNum;
    }

    public void setRouteNum(int routeNum) {
        this.routeNum = routeNum;
    }

    public StringProperty nameProperty() {
        return new SimpleStringProperty(name);
    }

    public IntegerProperty routeNumProperty() {
        return new SimpleIntegerProperty(routeNum);
    }

    //读取所有站台
    public HashMap<String, Station> obtainAllStation(){
        HashMap<String, Station> hashMap = new HashMap<>();
        String file = "src/main/java/org/example/ttms/file/station.txt";
        try {
            Scanner scan = new Scanner(new FileReader(file));
            while(scan.hasNextLine()){
                String str = scan.nextLine();
                ObjectMapper objectMapper = new ObjectMapper();
                Station station = objectMapper.readValue(str, Station.class);
                hashMap.put(station.name, station);
            }
        } catch (FileNotFoundException | JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return hashMap;
    }
    //添加站台
    public void addStation(Station station){
        ObjectMapper objectMapper = new ObjectMapper();
        String file = "src/main/java/org/example/ttms/file/station.txt";
        try {
            String jsonString = objectMapper.writeValueAsString(station);
            System.out.println("addStation:"+jsonString);
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter
                    (new FileOutputStream(file, true)));
            writer.write(jsonString+"\n");
            writer.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //重新储存站台信息---覆盖
    public void storageStation(ObservableList<Station> observableList){
        ObjectMapper objectMapper = new ObjectMapper();
        String file = "src/main/java/org/example/ttms/file/station.txt";
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new OutputStreamWriter
                    (new FileOutputStream(file)));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        for(Station station : observableList) {
            try {
                String jsonString = objectMapper.writeValueAsString(station);
                writer.write(jsonString + "\n");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        try {
            writer.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //返回指定名称的车站信息
    public List<Station> searchStation(String name){
        List<Station> list = new ArrayList<>();
        HashMap<String, Station> hashMap = obtainAllStation();
        for(Map.Entry<String, Station> map : hashMap.entrySet()){
            if(map.getKey().contains(name)){
                list.add(map.getValue());
            }
        }
        return list;
    }
    //删除指定名称的车站
    public void delStation(String name){
        HashMap<String, Station> hashMap = obtainAllStation();
        hashMap.remove(name);
        storageStation(FXCollections.observableArrayList(hashMap.values()));
    }
}
